package com.cuong004.android.FallDetector;

import com.example.android.navigationdrawerexample.R;
import com.jjoe64.graphview.CustomLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewDataInterface;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.Matrix;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class GraphFragmentIFall extends Fragment implements SensorEventListener, OnClickListener {
	GraphView graphView;
	GraphViewSeries exampleSeries1;
	SensorManager sensorManager;
	Sensor accSensor;
	TextView tv1;
	TextView tv2;
	TextView tv3;
	TextView tv4;
	TextView tv5;
	TextView tv6;
	Button bt1;
	double step = 6;
	double step1 = 6;
	GraphViewData[] data;
	GraphViewSeries seriesRnd;
	Sensor gsensor;
	Sensor rotation;
	// Accelerometter
	private float[] mGData = new float[4];
	// Magnetic
	private float[] mMData = new float[4];

	private float[] mR = new float[16];

	private float[] mI = new float[16];

	private float[] orientationvalues = new float[3];
	private float[] trueacceleration = new float[3];
	private GraphViewSeries exampleSeries2;
	public Activity activity = getActivity();
	protected boolean running = false;

	@SuppressLint("ValidFragment")
	public GraphFragmentIFall(Activity activity) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.graph_fragment, container, false);
		tv1 = (TextView) view.findViewById(R.id.tv1);
		tv2 = (TextView) view.findViewById(R.id.tv2);
		tv3 = (TextView) view.findViewById(R.id.tv3);
		tv4 = (TextView) view.findViewById(R.id.tv4);
		tv5 = (TextView) view.findViewById(R.id.tv5);
		tv6 = (TextView) view.findViewById(R.id.tv6);
		bt1 = (Button) view.findViewById(R.id.buttonStart);

		sensorManager = (SensorManager) activity
				.getSystemService(Context.SENSOR_SERVICE);
		accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
		// rotation = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

		exampleSeries1 = new GraphViewSeries("Random curve",
				new GraphViewSeriesStyle(Color.rgb(16, 66, 204), 3),
				new GraphViewData[] {});
		exampleSeries2 = new GraphViewSeries("Random curve",
				new GraphViewSeriesStyle(Color.rgb(0, 66, 133), 3),
				new GraphViewData[] {});

		seriesRnd = new GraphViewSeries("Random curve",
				new GraphViewSeriesStyle(Color.rgb(200, 50, 00), 3),
				new GraphViewData[] {});

		graphView = new LineGraphView(activity // context
				, "Accelerometter" // heading
		);

		graphView.addSeries(exampleSeries1); // data
		graphView.addSeries(seriesRnd);
		graphView.addSeries(exampleSeries2);
		graphView.setViewPort(0, 500);
		graphView.setCameraDistance(10);
		graphView.setManualYAxisBounds(50, -50);
		graphView.setCustomLabelFormatter(new CustomLabelFormatter() {

			@Override
			public String formatLabel(double value, boolean isValueX) {
				// TODO Auto-generated method stub
				if (isValueX) {
					if (value > 0) {
						return "0";
					}
				}
				return null; // let graphview generate Y-axis label for us
			}
		});

		graphView.setDisableTouch(true);
		graphView.setScalable(true);

		LinearLayout layout = (LinearLayout) view
				.findViewById(R.id.linearLayout1);
		layout.addView(graphView);
		
		 bt1.setOnClickListener(this);
		return view;
	}
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        activity = activity;
    }

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// sensorManager.registerListener(this, accSensor,
		// sensorManager.SENSOR_DELAY_NORMAL);
		// sensorManager.registerListener(this, gsensor,
		// SensorManager.SENSOR_DELAY_NORMAL);
//		sensorManager.registerListener(this, accSensor,
//				sensorManager.SENSOR_DELAY_GAME);
//		sensorManager.registerListener(this, gsensor,
//				SensorManager.SENSOR_DELAY_GAME);
		// sensorManager.registerListener(this, rotation,
		// SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub

		int type = event.sensor.getType();
		float[] data;
		if (type == Sensor.TYPE_ACCELEROMETER) {
			for (int i = 0; i < 3; i++)

				mGData[i] = event.values[i];
			tv4.setText("X: " + event.values[0]);
			tv5.setText("Y: " + event.values[1]);
			tv6.setText("Z: " + event.values[2]);

			// seriesRnd.appendData(
			// new GraphViewData(step += 0.5, event.values[2]), true, 500);

		} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {

			for (int i = 0; i < 3; i++)

				mMData[i] = event.values[i];

		} else if (type == Sensor.TYPE_ORIENTATION) {

			for (int i = 0; i < 3; i++) {
				orientationvalues[i] = event.values[i];
			}

		} else
			return;

		SensorManager.getRotationMatrix(mR, mI, mGData, mMData);
		SensorManager.getRotationMatrix(mR, mI, mGData, mMData);
		// float incl = SensorManager.getInclination(mI);
		// final float rad2deg = (float)(180.0f/Math.PI);
		float[] mInv = new float[16];

		Matrix.invertM(mInv, 0, mR, 0);
		Matrix.multiplyMV(mGData, 0, mInv, 0, mGData, 0);
		tv1.setText("X:"+ mGData[0]);
		tv2.setText("Y:" + mGData[1]);
		tv3.setText("Z:"+ mGData[2]);
		exampleSeries1.appendData(new GraphViewData(step1 += 0.5, mGData[2]),
				true, 1000);
		seriesRnd.appendData(new GraphViewData(step += 0.5,mGData[0]), true,
				1000);

	}
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		GraphViewDataInterface[] graphData = new GraphViewDataInterface[0];
		exampleSeries1.resetData( graphData);
		seriesRnd.resetData(graphData);
		sensorManager.unregisterListener(this);
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.buttonStart:
			if (running) {
				sensorManager.unregisterListener(this);
				bt1.setText("Start");
				bt1.setTextColor(getResources().getColor(R.color.blue));
				running = false;
			} else if (!running) {
				sensorManager.registerListener(this, accSensor, sensorManager.SENSOR_DELAY_GAME);
				sensorManager.registerListener(this, gsensor,
						SensorManager.SENSOR_DELAY_GAME);
				bt1.setText("Stop");
				bt1.setTextColor(getResources().getColor(R.color.red));
				running = true;
			}
			break;

		default:
			break;
		}
		
	}

}
